/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.facade;

import ar.com.neolokura.travian.helper.troop.entity.TroopDetail;
import ar.com.neolokura.travian.helper.bo.item.OasisItem;
import ar.com.neolokura.travian.helper.bo.object.LoginObject;
import ar.com.neolokura.travian.helper.bo.object.TroopSenderObject;

import ar.com.neolokura.travian.helper.troop.admin.TroopAdmin;
import ar.com.neolokura.travian.helper.user.admin.UserAdmin;
import ar.com.neolokura.travian.helper.world.admin.WorldAdmin;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author gustavo
 */
public class ServiceFacade {

    private static Logger logger = Logger.getLogger(ServiceFacade.class.getSimpleName());
    private static ServiceFacade INSTANCE = null;
    private UserAdmin userAdmin;
    private TroopAdmin troopAdmin;
    private WorldAdmin worldAdmin;

    // Private constructor suppresses 
    private ServiceFacade() {
        logger.info(">> ServiceFacade started...");
    }

    // creador sincronizado para protegerse de posibles problemas  multi-hilo
    // otra prueba para evitar instanciación múltiple 
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ServiceFacade();
        }
    }

    public static ServiceFacade getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    public LoginObject login(String userName, String password) throws Exception {
        userAdmin = UserAdmin.getInstance();
        logger.info("User " + userName + " logged!");
        return userAdmin.login(userName, password);
    }

    public Boolean isLogged() throws Exception {
        userAdmin = UserAdmin.getInstance();
        return userAdmin.isLogged(true);
    }

    public void sendTroops(TroopSenderObject senderObject) throws Exception {
        troopAdmin = TroopAdmin.getInstance();
        troopAdmin.sendTroops(senderObject);
        logger.info("Troops sent!");
    }

    public ArrayList<OasisItem> getAllOasis(Integer x, Integer y, Integer tilesAway) {
        worldAdmin = WorldAdmin.getInstance();
        ArrayList<OasisItem> oasisList = worldAdmin.getAllOasis(x, y, tilesAway);
        return oasisList;
    }

    public HashMap<String, TroopDetail> getIncomingTroops(ArrayList<Integer> villageIds) throws Exception {

        troopAdmin = TroopAdmin.getInstance();
        HashMap<String, TroopDetail> troops = troopAdmin.getIncomingTroops(villageIds);

        return troops;

    }
}
